import { UIMgr } from "../core/UIMgr";
import { Utils } from "../core/Utils";
import { DataMgr } from "../core/DataMgr";
import { Config } from "../core/Config";
import OnFire from "../core/OnFire";
import { Common } from "./Common";
import { JsonMgr } from "../core/JsonMgr";
import { ADMgr } from "../core/AdMgr";
import { Partner } from "../core/Partner";
import { AudioMgr } from "../core/AudioMgr";
import { BigNumber } from "./BigNumber";

cc.Class({
    extends: cc.Component,
    /***
     * 脚本组件执行优先级
     * 默认为0，可设置为负数
     * executionOrder值越小越优先执行
     */
    editor: {
        executionOrder: -1,
    },
    properties: {
        pfbToast: cc.Prefab,
    },

    onLoad() {
        if (window.G) {
            return;
        }
        window.G = window.G || {};
        this._initBigNumber();
        G.game = this;
        cc.game.addPersistRootNode(this.node);
        this._requireCores();
        this._initToastPool();

        this._boot();
    },

    _initBigNumber() {
        window.Bignumber = require('bignumber.min');
        Bignumber.config({ DECIMAL_PLACES: 5 });
    },

    _requireCores() {
        G.ui = UIMgr;
        G.utils = Utils;
        G.data = DataMgr;
        G.config = Config;
        G.onFire = new OnFire();
        G.common = Common;
        G.bignumber = BigNumber;
        G.json = JsonMgr;
        G.ad = ADMgr;
        G.ad.init();
        G.partner = Partner;
        G.partner.init();
        G.audio = AudioMgr;
        G.audio.init();
    },

    _boot() {
        cc.game.setFrameRate(G.config.FPS);
    },

    _initToastPool() {
        G.utils.createPool('nodePoolToast', this.pfbToast, 1);
    },

    setScene(scene) {
        this._scene = scene;
    },
    getScene() {
        return this._scene;
    },

    toast(content, delay) {
        this._scene.toast(content, delay);
    },

    loadScene(name, cb) {
        cc.director.loadScene(name, cb);
    },
    preloadScene(name, cb) {
        cc.director.preloadScene(name, cb);
    },

    // update (dt) {},
});
